package kite.springcloud.stream.kafka.producer.controller;

import kite.springcloud.common.stream.UserChannelProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserServiceController
 *
 * @author houxiurong
 * @date 2020/05/09
 */
@Slf4j
@RestController
public class UserServiceController {

    @Resource(name = UserChannelProcess.USER_TOPIC_OUTPUT)
    private MessageChannel sendUserMessageChannel;

    @GetMapping("/sendUserMsg")
    public String sendUserMessage(String content) {
        log.info("发送User消息内容:" + content);
        Message<String> message = MessageBuilder.withPayload(content)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        boolean isUserSendSuccess = sendUserMessageChannel.send(message);
        log.info("isSendSuccess={},send message={}", isUserSendSuccess, message);
        return isUserSendSuccess ? "user消息发送成功" : "user消息发送失败";
    }

}